package com.changyinBus.system.service.impl;

import com.changyinBus.common.core.domain.entity.SysArticle;
import com.changyinBus.system.mapper.SysArticleMapper;
import com.changyinBus.system.service.ISysArticleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

@Service
public class SysArticleServiceImpl implements ISysArticleService {
    @Resource
    private SysArticleMapper sysArticleMapper;

    @Override
    public List<SysArticle> selectArticleList(SysArticle article) {
        return sysArticleMapper.selectArticleList(article);
    }

    @Override
    public List<SysArticle> selectArticleListByTopWithLimit(long tenantId, int articleType, int limit) {
        return sysArticleMapper.selectArticleListByTopWithLimit(tenantId, articleType, limit);
    }

    @Override
    public SysArticle selectArticleById(Long articleId) {
        return sysArticleMapper.selectArticleById(articleId);
    }

    @Override
    public SysArticle selectArticleByArticleType(int articleType, long typeId, long tenantId) {
        return sysArticleMapper.selectArticleByArticleType(articleType, typeId, tenantId);
    }

    @Override
    public int insertArticle(SysArticle article) {
        try {
            if(article.getContent() != null && !article.getContent().trim().equals("")) {
                //转译带style的content
                article.setContent(new String(Base64.getDecoder().decode(article.getContent()), "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return sysArticleMapper.insertArticle(article);
    }

    @Override
    public int updateArticle(SysArticle article) {
        try {
            if(article.getContent() != null && !article.getContent().trim().equals("")) {
                //转译带style的content
                article.setContent(new String(Base64.getDecoder().decode(article.getContent()), "UTF-8"));
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return sysArticleMapper.updateArticle(article);
    }

    @Override
    public int updateArticleStatus(SysArticle article) {
        return sysArticleMapper.updateArticleStatus(article.getArticleId(), article.getStatus());
    }

    @Override
    public int deleteArticleById(Long articleId) {
        return sysArticleMapper.deleteArticleById(articleId);
    }

    @Override
    public SysArticle getPrevious(SysArticle article) {
        return sysArticleMapper.getPrevious(article);
    }

    @Override
    public SysArticle getNext(SysArticle article) {
        return sysArticleMapper.getNext(article);
    }
}
